package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.EndlessInfo;
import com.baomidou.ant.entity.QuestionInfo;
import com.baomidou.ant.mapper.EndlessInfoMapper;
import com.baomidou.ant.service.IEndlessInfoService;
import com.baomidou.ant.service.IQuestionInfoService;
import com.baomidou.ant.utils.MyStringUtils;
import com.baomidou.ant.vo.Request.RequestChoiceInfoVo;
import com.baomidou.ant.vo.Response.ResponseEndlessQuestionVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@Service
public class EndlessInfoServiceImpl extends ServiceImpl<EndlessInfoMapper, EndlessInfo> implements IEndlessInfoService {
    @Autowired
    private IQuestionInfoService questionInfoService;

    @Override
    public ResponseEndlessQuestionVo getQuestion(Integer userId) {
        QueryWrapper<EndlessInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        EndlessInfo endlessInfo=getOne(queryWrapper);
        //如果用户没开始，则进入第一关
        if(endlessInfo.getNowLevel()==0){
            endlessInfo.setNowLevel(1);
            saveOrUpdate(endlessInfo);
        }
        //这一步是为了避免用户卡bug，防止用户通过刷新页面的方式刷简单题，可结合openGame中判断异常刷新的语句即可理解
        if(Objects.equals(endlessInfo.getNowLevel(), endlessInfo.getLastLevel())){
            endlessInfo.setLastLevel(endlessInfo.getNowLevel()-1);
            saveOrUpdate(endlessInfo);
        }
        //先拿到所有题目
        List<QuestionInfo> allQuestion=questionInfoService.list();
        //随机生成要拿题目的id
        Random random=new Random();
        int result = random.nextInt(allQuestion.size());
        //拿到做过的题目
        int[] doneQuestion= MyStringUtils.toInt(endlessInfo.getNowDone());
        //依次存入list中
        List<Integer> doneList = Arrays.stream(doneQuestion).boxed().collect(Collectors.toList());
        //判断是否做过这个题目
        for(int i=0;i<=allQuestion.size();i++){
            if(doneList.contains(result)||result<=0){
                //重拿result
                result=random.nextInt(allQuestion.size());
            }else
                break;
        }
        //得到题目
        QuestionInfo question=questionInfoService.getById(result);
        ResponseEndlessQuestionVo vo=new ResponseEndlessQuestionVo();
        BeanUtils.copyProperties(question,vo);
        vo.setNowLevel(endlessInfo.getNowLevel());
        vo.setErrorNum(endlessInfo.getErrorNum());
        //该关卡的剩余时间
        vo.setNowTime(endlessInfo.getNowTime());
        return vo;
    }

    @Override
    public boolean trueOrFalse(RequestChoiceInfoVo vo) {
        //根据题目id找到对应题目
        QuestionInfo questionInfo=questionInfoService.getById(vo.getQuestionId());
        //对比答案,返回结果
        return vo.getAnswer().equals(questionInfo.getRightItem());
    }

    @Override
    public EndlessInfo queryByUserId(Integer userId) {
        QueryWrapper<EndlessInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<EndlessInfo> endlessInfo=list(queryWrapper);
        return endlessInfo.get(0);
    }
}
